using DailyPoetryH.Library.Models;
using DailyPoetryH.Library.Services;

namespace DailyPoetryH.Library.Pages;

public partial class Favorite {
    private List<PoetryFavorite> _poetryFavorites = new();

    private string _status = String.Empty;

    public const string Loading = "正在载入";

    public const string NoMoreResult = "没有更多结果";

    protected override async Task OnAfterRenderAsync(bool firstRender) {
        if (!firstRender) {
            return;
        }

        _status = Loading;
        _poetryFavorites.Clear();
        StateHasChanged();

        var favorites = await _favoriteStorage.GetFavoritesAsync();
        var tasks = favorites.Select(p => Task.Run(async () =>
            new PoetryFavorite {
                Poetry = await _poetryStorage.GetPoetryAsync(p.PoetryId),
                Favorite = p
            }));
        var poetryFavorites = await Task.WhenAll(tasks);
        _poetryFavorites.AddRange(poetryFavorites);
        _status = NoMoreResult;
        StateHasChanged();
    }

    private void OnClick(Poetry poetry) =>
        _navigationService.NavigateTo(
            $"{NavigationServiceConstants.DetailPage}/{poetry.Id}");
}

public class PoetryFavorite {
    public Poetry Poetry { get; set; }

    public Models.Favorite Favorite { get; set; }
}